package com.cskaoyan.controller;

import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.domain.MarketStorage;
import com.cskaoyan.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/wx/storage")
public class WxStorageController {
    @Autowired
    StorageService storageService;
    @PostMapping("upload")
    public BaseRespVo upload( @RequestParam("file") MultipartFile file) {
            String randomFileName = UUID.randomUUID().toString().replaceAll("-", "");
            String originalFilename = file.getOriginalFilename();
            int index = originalFilename.lastIndexOf(".");
            String fileNameSuffix = originalFilename.substring(index);
            String fileName = randomFileName + fileNameSuffix;
            File saveFile = new File("d:/storage", fileName);
            try {
                file.transferTo(saveFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            MarketStorage data = storageService.store(file, fileName);
            return BaseRespVo.ok(data);
    }
}
